package com.chenyue.cm.user.service.impl;


import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.chenyue.cm.base.service.BaseServiceImpl;
import com.chenyue.cm.base.dao.LcBaseMapper;
import com.chenyue.cm.bean.Success;
import com.chenyue.cm.common.utils.StringUtils;
import com.chenyue.cm.user.domain.JdGoodsSku;
import com.chenyue.cm.user.service.JdGoodsSkuService;
import com.chenyue.cm.utils.DateUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import com.chenyue.cm.user.dao.JdMemberShoppingCartMapper;
import com.chenyue.cm.user.domain.JdMemberShoppingCart;
import com.chenyue.cm.user.service.JdMemberShoppingCartService;
import org.springframework.stereotype.Service;


/**
*
* @author chenyue-cm
*/
@Service(value = "JdMemberShoppingCartService")
public class JdMemberShoppingCartServiceImpl extends BaseServiceImpl<JdMemberShoppingCart>  implements JdMemberShoppingCartService {

private final JdMemberShoppingCartMapper jdMemberShoppingCartMapper;

@Resource
private JdGoodsSkuService goodsSkuService;

	public JdMemberShoppingCartServiceImpl(@Qualifier("jdMemberShoppingCartMapper") JdMemberShoppingCartMapper jdMemberShoppingCartMapper) {
		this.jdMemberShoppingCartMapper = jdMemberShoppingCartMapper;
	}

	@Override
	@Resource(name = "jdMemberShoppingCartMapper")
	public void setBaseMapper(LcBaseMapper<JdMemberShoppingCart, Serializable> baseMapper) {
		this.baseMapper = baseMapper;
	}

	@Override
	public Success<String> addCart(String id, String goodsId, String skuId, String selectedNum) {
		JdMemberShoppingCart jdMemberShoppingCart = new JdMemberShoppingCart();
		jdMemberShoppingCart.setGoodsId(goodsId);
		jdMemberShoppingCart.setMemberId(id);
		LambdaQueryWrapper<JdGoodsSku> wrapper = new LambdaQueryWrapper<>();
		if (StringUtils.isNotEmpty(skuId) && !skuId.equals("undefined")) {
			wrapper.eq(JdGoodsSku::getGoodsId, goodsId).eq(JdGoodsSku::getId, skuId);
		} else {
			wrapper.eq(JdGoodsSku::getGoodsId, goodsId);
		}
		JdGoodsSku sku = goodsSkuService.getOne(wrapper);


		// 获取商品规格名
		String attrInfo = null;
		if (sku != null) {
			JSONArray array = JSON.parseArray(sku.getAttrInfo());
			if (array != null && array.size() > 0) {
				List<String> att = new ArrayList<>();
				for (int i = 0; i < array.size(); i++) {
					JSONObject job = array.getJSONObject(i);
					String value = job.getString("value");
					att.add(value);
				}
				attrInfo = String.join(",", att);
			}
			jdMemberShoppingCart.setSkuId(sku.getId());
			jdMemberShoppingCart.setPrice(sku.getSkuSaleprice());
		} else {
			LambdaQueryWrapper<JdGoodsSku> w = new LambdaQueryWrapper<>();
			w.eq(JdGoodsSku::getGoodsId, goodsId);
			JdGoodsSku goodsSku = goodsSkuService.getOne(w);
			jdMemberShoppingCart.setPrice(goodsSku.getSkuSaleprice());
		}

		JdMemberShoppingCart cart = this.getById(jdMemberShoppingCart);
		if (cart != null) {
			if (sku.getSkuStock() < (Integer.parseInt(selectedNum) + cart.getPurchaseNum())) {
				return Success.error("库存不足");
			}
			cart.setPurchaseNum(Integer.parseInt(selectedNum) + cart.getPurchaseNum());
			cart.setUpdateTime(DateUtils.getTimeNow());
			cart.setCreateTime(DateUtils.getTimeNow());
			cart.setSkuAttrInfo(attrInfo);
			this.updateById(cart);
		} else {
			if (sku.getSkuStock() < Integer.parseInt(selectedNum)) {
				return Success.error("库存不足");
			}
			jdMemberShoppingCart.setId(UUID.randomUUID().toString().replace("-", ""));
			jdMemberShoppingCart.setSkuAttrInfo(attrInfo);
			jdMemberShoppingCart.setPurchaseNum(Integer.parseInt(selectedNum));
			jdMemberShoppingCart.setCreateTime(DateUtils.getTimeNow());
			this.save(jdMemberShoppingCart);
		}
		return Success.success();
	}

}
